package com.siam.package_promotion.service_impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siam.package_promotion.entity.DeductionMemberRelation;
import com.siam.package_promotion.mapper.DeductionMemberRelationMapper;
import com.siam.package_promotion.service.DeductionMemberRelationService;

/**
 * ---------------------------
 * 抵扣劵用户关联表 (DeductionMemberRelationServiceImpl)         
 * ---------------------------
 * 作者：  暹罗
 * 时间：  2025-02-25 15:21:52
 * ---------------------------
 */
@Service
public class DeductionMemberRelationServiceImpl implements DeductionMemberRelationService {

	@Autowired
	private DeductionMemberRelationMapper tbDeductionMemberRelationMapper;

	@Override
	public void insertSelective(DeductionMemberRelation memberRelation) {
		memberRelation.setCreateTime(new Date());
		memberRelation.setUpdateTime(new Date());
		tbDeductionMemberRelationMapper.insert(memberRelation);
	}

	@Override
	public int countByOrderId(Integer orderId) {
		LambdaQueryWrapper<DeductionMemberRelation> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(DeductionMemberRelation::getOrderId, orderId);
		queryWrapper.eq(DeductionMemberRelation::getIsValid, 1);
		return tbDeductionMemberRelationMapper.selectCount(queryWrapper);
	}

	@Override
	public Page<DeductionMemberRelation> getListByPage(Page<DeductionMemberRelation> page,
			DeductionMemberRelation deductionMemberRelation) {
		LambdaQueryWrapper<DeductionMemberRelation> queryWrapper = Wrappers.lambdaQuery(deductionMemberRelation);
		queryWrapper.eq(DeductionMemberRelation::getIsValid, 1);
		return tbDeductionMemberRelationMapper.selectPage(page, queryWrapper);
	}

	@Override
	public Integer getCountsByMemberId(Integer memberId, Integer isUsed) {
		LambdaQueryWrapper<DeductionMemberRelation> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(DeductionMemberRelation::getIsValid, 1);
		queryWrapper.eq(DeductionMemberRelation::getMemberId, memberId);
		queryWrapper.eq(DeductionMemberRelation::getIsUsed, isUsed);
		return tbDeductionMemberRelationMapper.selectCount(queryWrapper);
	}

	@Override
	public DeductionMemberRelation selectNotUsedByDeductionMemberRelationId(Integer deductionMemberRelationId) {
		LambdaQueryWrapper<DeductionMemberRelation> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(DeductionMemberRelation::getIsValid, 1);
		queryWrapper.eq(DeductionMemberRelation::getId, deductionMemberRelationId);
		queryWrapper.eq(DeductionMemberRelation::getIsUsed, 0);
		return tbDeductionMemberRelationMapper.selectOne(queryWrapper);
	}

	@Override
	public void updateDeductionUsed(Integer id, Boolean isUsed) {
		DeductionMemberRelation memberRelation = new DeductionMemberRelation();
		memberRelation.setId(id);
		memberRelation.setIsUsed(isUsed?1:0);
		memberRelation.setUpdateTime(new Date());
		tbDeductionMemberRelationMapper.updateById(memberRelation);
	}

}
